//var ctx = "/";
var attachRoot = ctx + "/attachments/";

/**
 * 根据URL的参数名获取URL中的参数值
 * 
 * @param paras
 * @returns
 */
function request(paras) {
	var url = location.href;
	var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
	var paraObj = {};
	for ( var i = 0; j = paraString[i]; i++) {
		paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j
				.indexOf("=") + 1, j.length);
	}
	var returnValue = paraObj[paras.toLowerCase()];
	if (typeof (returnValue) == "undefined") {
		return "";
	} else {
		return returnValue;
	}
}


/**
 * 获得状态名称
 * 
 * @param code
 * @returns {String}
 */
function getTaskStatus(code) {
	return {
		"draft" : "草稿",
		"assigned" : "待办",
		"in_process" : "处理中",
		"completed" : "已完成"
	}[code];
}

/**
 * 获得创建类型名称
 * @param code
 * @returns {String}
 */
function getCreateType(code){
	return {
		"dispatch" : "派发任务",
		"self" : "自主任务"
	}[code];
}

/**
 * 获得签到类型名称
 * 
 * @param code
 * @returns {String}
 */
function getCheckinType(code) {
	return {
		"on-duty" : "上班",
		"off-duty" : "下班"
	}[code];
}

/**
 * 获得访问客户端名称
 * 
 * @param code
 * @returns {String}
 */
function getClientBy(code) {
	return {
		"by-web" : "网页",
		"by-mobile" : "手机"
	}[code];
}


/**
 * 无提示关闭窗口
 */
function closeWindow() {
	window.opener = null;
	window.open('', '_self');
	window.close();
}

/**
 * 返回前一页。对于弹出窗口或前一页是登录窗口的，返回指定的连接
 */
function goBack(defaultUrl) {
	if (defaultUrl == undefined) {
		history.go(-1);
		return;
	}
	if (window.opener != undefined
			|| document.referrer.indexOf("login.html") > 0) {
		location.href = defaultUrl;
		return;
	}
	history.go(-1);
}

/**
 * 格式化数字 
 * 
 * 示例： 
 * alert(formatNumber(0,'')); 
 * alert(formatNumber(12432.21,'#,###'));
 * alert(formatNumber(12432.21,'#,###.000#'));
 * alert(formatNumber(12432,'#,###.00'));
 * alert(formatNumber('12432.415','#,###.0#'));
 */
function formatNumber(number, pattern) {
	if(number == undefined){
		return '';
	}
	var str = number.toString();
	var strInt;
	var strFloat;
	var formatInt;
	var formatFloat;
	if (/\./g.test(pattern)) {
		formatInt = pattern.split('.')[0];
		formatFloat = pattern.split('.')[1];
	} else {
		formatInt = pattern;
		formatFloat = null;
	}

	if (/\./g.test(str)) {
		if (formatFloat != null) {
			var tempFloat = Math.round(parseFloat('0.' + str.split('.')[1])
					* Math.pow(10, formatFloat.length))
					/ Math.pow(10, formatFloat.length);
			strInt = (Math.floor(number) + Math.floor(tempFloat)).toString();
			strFloat = /\./g.test(tempFloat.toString()) ? tempFloat.toString()
					.split('.')[1] : '0';
		} else {
			strInt = Math.round(number).toString();
			strFloat = '0';
		}
	} else {
		strInt = str;
		strFloat = '0';
	}
	if (formatInt != null) {
		var outputInt = '';
		var zero = formatInt.match(/0*$/)[0].length;
		var comma = null;
		if (/,/g.test(formatInt)) {
			comma = formatInt.match(/,[^,]*/)[0].length - 1;
		}
		var newReg = new RegExp('(\\d{' + comma + '})', 'g');

		if (strInt.length < zero) {
			outputInt = new Array(zero + 1).join('0') + strInt;
			outputInt = outputInt.substr(outputInt.length - zero, zero)
		} else {
			outputInt = strInt;
		}

		var outputInt = outputInt.substr(0, outputInt.length % comma)
				+ outputInt.substring(outputInt.length % comma).replace(newReg,
						(comma != null ? ',' : '') + '$1')
		outputInt = outputInt.replace(/^,/, '');

		strInt = outputInt;
	}

	if (formatFloat != null) {
		var outputFloat = '';
		var zero = formatFloat.match(/^0*/)[0].length;

		if (strFloat.length < zero) {
			outputFloat = strFloat + new Array(zero + 1).join('0');
			// outputFloat = outputFloat.substring(0,formatFloat.length);
			var outputFloat1 = outputFloat.substring(0, zero);
			var outputFloat2 = outputFloat.substring(zero, formatFloat.length);
			outputFloat = outputFloat1 + outputFloat2.replace(/0*$/, '');
		} else {
			outputFloat = strFloat.substring(0, formatFloat.length);
		}

		strFloat = outputFloat;
	} else {
		if (pattern != '' || (pattern == '' && strFloat == '0')) {
			strFloat = '';
		}
	}

	return strInt + (strFloat == '' ? '' : '.' + strFloat);
}

/**
 * 判断是否有位置信息
 * @param task
 */
function hasLocation(task){
	if(task == undefined){
		return false;
	}
	var lon = task.longitude;
	var lat = task.latitude;
	if(lon == undefined || lat == undefined || lon == 0 || lat == 0){
		return false;
	}
	return true;
}

/**
 * 屏蔽
 * @param jqe	被屏蔽的元素
 * @param message	提示消息
 */
function block(jqe, message) {
	if(message == undefined || message == ""){
		message = "处理中，请稍候...";
	}
	$("#blockDiv").find("span").text(message);
	$(jqe).block({
		fadeIn:	200,
		fadeOut: 400, 
		//timeout: 3000,
		ignoreIfBlocked: true, 
		message: $("#blockDiv")
	}); 
}

/**
 * 取消屏蔽
 */
function unblock(jqe){
	$(jqe).unblock();
}

/**
 * 将省份加载到指定select中
 * @param selectId
 * @param callback
 */
function loadProvinces(selectId, callback) {
	$.ajax({
    	type: "GET",
    	cache: false,
    	url: ctx + "/biz/dict/area/list.json",
    	dataType:"json",
    	success:function(json) {
    		if (!json.succeed) {
    			$.alert(json.message);
    			return;
    		}
    		$(json.data).each(function(index, item){
    			$("#" + selectId).append("<option value='" + item.id + "'>" + item.name + "</option>");
    		});
    		if(typeof(callback) === "function") {
    			callback();
    		}
    	},
		error: function(message) {
			$.alert(message);
		}
	});
}

/**
 * 处理新消息提示
 */
function getNewMessages() {
	var countSpan = $("#myNewMsgCount");
	if (countSpan.length == 0) {
		return;
	}
	
	$.ajax({
    	type: "GET",
    	cache: false,
    	url: ctx + "/biz/message/news.json",
    	dataType:"json",
    	success:function(json) {
    		if (!json.succeed) {
    			//$.alert(json.message);
    			return;
    		}
    		var count = json.data.count;
    		if (count == 0) {
    			countSpan.hide();
    			$("#myNewMessages").hide();
    		} else {
    			countSpan.text(count);
    			countSpan.show();
    			
    			var html = '';
    			$(json.data.list).each(function(index, item) {
    				html = html + '<li><a href="' + ctx + '/biz/message/read.do?id=' + item.id 
    				+ '"><span class="label label-warning">' + item.name + '</span></a></li>';
    			});
    			$("#myNewMessages").html(html);
    			$("#myNewMessages").show();
    			$("#myNewMessages").marquee();
    		}
    	},
		error: function(message) {
			//$.alert(message);
		}
	});
}
// 定时获得新消息
$(document).ready(function() {  
	$('body').everyTime('1s', getNewMessages, 1);
	$('body').everyTime('100s', getNewMessages, 0, true);
});